import { createRouter, createWebHashHistory } from 'vue-router';
import { constantRoute, asyncRoute } from './routes';
import { useMain } from '@/store/modules/user'; // 引入st
import { toRaw } from 'vue';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [...constantRoute],
  // 滚动行为
  scrollBehavior() {
    return {
      left: 0,
      top: 0,
    };
  },
});

router.beforeEach((to, from, next) => {
  console.log(to);
  if (to.fullPath === '/home') {
    next();
    return;
  }
  if (Window.IS_FLISH) {
    next();
    return;
  } else {
    console.log('add route');
    const useStoreMain = useMain();
    const { menuRoutes } = useStoreMain;
    menuRoutes.forEach((menu) => {
      router.addRoute(menu);
      router.options.routes.push(toRaw(menu));
    });
    Window.IS_FLISH = true;
    next();
    console.log(router.options);
  }
});

export default router;
